package StringStudy;

import java.util.Random;
import java.util.Scanner;

/**
 * 需求:定义一个int数组,按照指定的格式拼成字符串返回{1, 2, 3}-->[1,2,3]
 *
 *
 * 输入一个字符串，并打乱顺序
 * absdfds->sdfklds
 * 1. 字符串转换成字符数组--》toCharArray  字符串转成字符数组
 *      字符数组乱序 下标 随机值 //0
 *        for(int i=0;i<arr.length;i++)
 *        {
 *        int rd=random.nextInt(arr.length)
 *            r=arr[i];
 *            arr[i]=rd;
 *            rd=r;
 *
 *        }
 *
 * 2.把乱序的字符数组 数转成字符串返回
 *   new String(arr)
 */
public class StringLearn2 {
    public static void main(String[] args) {
//        Random random = new Random();
//        random.nextInt(8)
        System.out.println("请输入你的手机号");
        Scanner scanner = new Scanner(System.in);
        String tel = scanner.next();
        String res = "";

        tel = tel.replace(tel.substring(3, 7), "****");
        for (int i = 0; i < tel.length(); i++) {
            if(i>=3 && i<7)
            {
                res += '*';
            }else {
                res += tel.charAt(i);
            }
        }
        System.out.println("res = " + res);
        // System.out.println("reverse(\"xyzabc\") = " + reverse("xyzabc"));
     //   int[] arr = {1, 2, 3};
     //   System.out.println("arrToStr(arr) = " + arrToStr(arr));
//        StringLearn2 stringLearn2 = new StringLearn2();
//        System.out.println("stringLearn2.arrToString(arr) = " + stringLearn2.arrToString(arr));


    }

    /**
     *
     * 实用StringBuilder实现数组转换成字符串
     */


    public  String arrToString(int [] arr)
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < arr.length; i++) {
            if(arr.length-1==i)
            {
                stringBuilder.append(arr[i]);
            }else {
                stringBuilder.append(arr[i]).append(",");
            }
        }
        stringBuilder.append("]");
//        System.out.println("stringBuilder = " + stringBuilder);
        return stringBuilder.toString();

    }

    /**
     * 数组转成指定格式的字符串
     * @param arr
     * @return
     */
    public static String arrToStr(int [] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                str += arr[i];
            } else {
                str += arr[i] + ",";
            }
        }
        str += "]";
        return str;
    }

    /**
     * 字符串逆置1
     *
     * @param str
     * @return
     */
    public static String reverse(String str) {//axyz;
        //zy
        String res = "";
        for (int i = 0; i < str.length(); i++) {
            res += str.charAt(str.length() - i - 1);
        }
        return res;
    }

    /**
     * 字符串逆置2
     *
     * @param str
     * @return
     */
    public static String reverse1(String str) {
        String res = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            res += str.charAt(i);
        }
        return res;
    }
}
